// pages/index/index2.js

const AUTH_MODE = "fingerPrint";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wifissid: "",
    wifiBssid: "",
    ipp: "探测IP地址", //IP地址前几位
    ip: "IP地址", //IP地址
    ip_address: [], //IP地址
    port: 10000, //端口号
    strs: "", //接收到的数据
    message: "", //发送的数据
  },





  startAuth() {
    const startSoterAuthentication = () => {
      wx.startSoterAuthentication({
        requestAuthModes: [AUTH_MODE],
        challenge: "test",
        authContent: "生物认证",
        success: (res) => {
          wx.showToast({
            title: "认证成功",
          }),
            /**查询终端设备 */
            wx.showLoading({
              title: "加载中",
            });
          var num = 0;
          var n = 0;
          this.data.ipp = "192.168.43.";

          const udp = wx.createUDPSocket();

          let that = this;
          udp.onMessage(function (res) {
            console.log(res);
            console.log(res.message);
            var buf = res.message;
            /**数据类型转换 */
            var decodedString = String.fromCharCode.apply(
              null,
              new Uint8Array(buf)
            );
            let str = decodedString;
            console.log("str===" + str);
            console.log(that.data.ip)
            that.data.strs = str;
            console.log(that.data.strs);



            var address = null
            address = that.data.strs.split("|")
            that.data.ip_address[n] = address[1]
            console.log("that.data.ip_address[n] is : " + that.data.ip_address[n]);
            getApp().globalData.ip = that.data.ip_address[n]
            getApp().globalData.port = that.data.port
            console.log("getApp().globalData.ip is : " + getApp().globalData.ip)
            console.log("getApp().globalData.port is : " + getApp().globalData.port)
            n = n + 1

          });

          while (num <= 254) {
            num = num + 1;
            this.data.ip = this.data.ipp + num.toString();
            console.log(this.data.ip)
            // 发送UDP字节函数
            udp.bind(10000);
            udp.send({
              address: this.data.ip,
              port: this.data.port,
              length: 1024,
              message: "I am Ready",
            });
          }
          // udp.offMessage()
          // udp.close()
          // setTimeout(function () {
          wx.hideLoading(); //必须调用hideLoading才能关闭loading
          // }, 2000);
          /**关闭页面 */
          wx.redirectTo({
            url: "../test1/test",
          });
        },
        fail: (err) => {
          console.error(err);
          wx.showModal({
            title: "失败",
            content: "认证失败",
            showCancel: false,
          });
        },
      });
    };

    const checkIsEnrolled = () => {
      wx.checkIsSoterEnrolledInDevice({
        checkAuthMode: AUTH_MODE,
        success: (res) => {
          console.log(res);
          if (parseInt(res.isEnrolled) <= 0) {
            wx.showModal({
              title: "错误",
              content: "您暂未录入指纹信息，请录入后重试",
              showCancel: false,
            });
            return;
          }
          startSoterAuthentication();
        },
        fail: (err) => {
          console.error(err);
        },
      });
    };

    wx.checkIsSupportSoterAuthentication({
      success: (res) => {
        console.log(res);
        checkIsEnrolled();
      },
      fail: (err) => {
        console.error(err);
        wx.showModal({
          title: "错误",
          content: "您的设备不支持指纹识别",
          showCancel: false,
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.startWifi({
      success(res) {
        console.log(res.errMsg, "wifi初始化成功");
      },
      fail: function (res) {
        console.log(res.errMsg, "wifi初始化失败");
      },
    });
    // wx.connectWifi({
    //   SSID: '',
    //   password: '',
    //   success(res) {
    //     console.log(res.errMsg, 'wifi连接成功')
    //   },
    //   fail: function(res){
    //     console.log(res.errMsg, 'wifi连接失败')
    //   }
    // })
    let that = this;
    wx.getConnectedWifi({
      success: function (e) {
        console.log(e.wifi, "wifi获取成功");
        that.setData({
          wifissid: e.wifi.SSID,
          wifiBssid: e.wifi.BSSID,
        });
        // console.log(e.wifi.SSID)
        // console.log(e.wifi.BSSID)
        getApp().globalData.wifissid = e.wifi.SSID;
        getApp().globalData.wifiBssid = e.wifi.BSSID;
        console.log(
          getApp().globalData.wifissid,
          getApp().globalData.wifiBssid
        );
      },
      fail: function (e) {
        console.log(e, "wifi获取失败");
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.checkIsSoterEnrolledInDevice({
      checkAuthMode: "fingerPrint",
      success(res) {
        console.log(res.isEnrolled);
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    udp.offMessage()
    udp.close()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
});
